package com.xiaotu.spider.sohu.pageprocessor;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.HotTVModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.PropertiesUtil;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

/**
 * @类名 SohuHotPageProcessor
 * @日期 2017年2月13日
 * @作者 高海军
 * @功能 搜狐网站热剧爬取
 */
@Component("SohuHotPageProcessor")
@Scope("prototype")
public class SohuHotPageProcessor extends AUrlPageProcessor
{
	@Override
	protected String[] getStartRequestUrls()
	{
		return new String[] {
				"http://so.tv.sohu.com/list_p1101_p20_p31000_p40_p5_p6_p74_p80_p91_p10_p11_p12_p13.html", // 内地最热
				"http://so.tv.sohu.com/list_p1101_p20_p31000_p40_p5_p6_p73_p80_p91_p10_p11_p12_p13.html",// 内地最新
                "http://so.tv.sohu.com/list_p1101_p20_p31001_p40_p5_p6_p74_p80_p91_p10_p11_p12_p13.html",//香港最热
                "http://so.tv.sohu.com/list_p1101_p20_p31001_p40_p5_p6_p73_p80_p91_p10_p11_p12_p13.html",//香港最新
                "http://so.tv.sohu.com/list_p1101_p20_p31002_p40_p5_p6_p77_p80_p91_p10_p11_p12_p13.html",//台湾最热
                "http://so.tv.sohu.com/list_p1101_p20_p31002_p40_p5_p6_p73_p80_p91_p10_p11_p12_p13.html"//台湾最新
		};
	}

	@Override
	protected String getSpiderType()
	{
		return Constants.SpiderTaskType.SOHU_HOT;
	}

	@Override
	public String getPageDataType(Page page)
	{
		return Constants.PageDataType.SEARCH_LIST;
	}

	@Override
	public void process(Page page)
	{
		List<String> divList = page.getHtml()
				.xpath("//div[@class='column-bd cfix']/ul[@class='st-list cfix']/li")
				.all();
		if (divList == null || divList.isEmpty())
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);

		List<HotTVModel> tvList = new ArrayList<HotTVModel>();
		for (String divStr : divList)
		{
			Html divObj = new Html(divStr);
			tvList.add(new HotTVModel(Constants.SiteName.SOHU,
					divObj.xpath("//strong/a/@title").get(),
					divObj.xpath("//p[@class='actor']/a/@title").all(),
					divObj.xpath("//strong/a/@href").get()));
		}
		page.putField(HotTVModel.class.getName(), tvList);

		String current = page.getHtml()
				.xpath("//div[@class='ssPages area']/span/text()").get();
		if (StringUtils.isNotEmpty(current))
		{
			current = RegexUtils.regexSearch(RegexUtils.REGEX_INTEGER, current);
			if (StringUtils.isNotEmpty(current))
			{
				if (PropertiesUtil.getPropertyByInt(
						Constants.SpiderConfigKey.HOTTV_PAGE_SIZE) > Integer
								.parseInt(current))
					PageUtils.addTargetRequest(page,
							page.getHtml()
									.xpath("//div[@class='ssPages area']/a[@title='下一页']/@href")
									.get(),
							page.getRequest().getExtras());
			}
		}
	}
}
